package com.manager.service.impl;

import com.manager.bean.PageInfo;
import com.manager.bean.Repair;
import com.manager.bean.RepairExample;
import com.manager.mapper.RepairMapper;
import com.manager.service.RepairService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (Repair)表服务实现类
 *
 * @author makejava
 * @since 2021-03-02 11:00:25
 */
@Service("repairService")
public class RepairServiceImpl implements RepairService {
    @Resource
    private RepairMapper repairDao;

    @Override
    public PageInfo<Repair> findPageInfo(Integer DormitoryId, Integer StudentId, Integer pageIndex, Integer pageSize) {
        PageInfo<Repair> pi = new PageInfo<Repair>();
        pi.setPageIndex(pageIndex);
        pi.setPageSize(pageSize);
        RepairExample RepairExample=new RepairExample();
        RepairExample.Criteria criteria=RepairExample.createCriteria();
        if(DormitoryId!=null){
            criteria.andDormitoryIdEqualTo(DormitoryId);
        }
        if(StudentId!=null){
            criteria.andStudentIdEqualTo(StudentId);
        }
        //获取总条数
        Integer totalCount = repairDao.countByExample(RepairExample);
        if (totalCount>0){
            pi.setTotalCount(totalCount);
            //每一页显示学生信息数
            //currentPage = (pageIndex-1)*pageSize  当前页码数减1*最大条数=开始行数
            List<Repair> Repairs = repairDao.selectByExample(RepairExample);
            int start=(pi.getPageIndex()-1)*pi.getPageSize();
            int end=start+pi.getPageSize();
            if(end>Repairs.size()-1){
                end=Repairs.size();
            }
            Repairs=Repairs.subList(start,end);
            pi.setList(Repairs);
        }
        return pi;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Repair queryById(Integer id) {
        return this.repairDao.selectByPrimaryKey(id);
    }


    @Override
    public List<Repair> getAll(){
        RepairExample repairExample = new RepairExample();
        RepairExample.Criteria criteria = repairExample.createCriteria();
        return repairDao.selectByExample(repairExample);
    }

    /**
     * 新增数据
     *
     * @param repair 实例对象
     * @return 实例对象
     */
    @Override
    public Repair insert(Repair repair) {
        this.repairDao.insert(repair);
        return repair;
    }

    /**
     * 修改数据
     *
     * @param repair 实例对象
     * @return 实例对象
     */
    @Override
    public Repair update(Repair repair) {
        repair.setCompleteTime(new Date(new Date().getTime()));
        this.repairDao.updateByPrimaryKeySelective(repair);
        return this.queryById(repair.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.repairDao.deleteByPrimaryKey(id) > 0;
    }
}
